package vn.edu.saigontech.ncms.web.student;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import vn.edu.saigontech.ncms.model.result.PlacementShift;
import vn.edu.saigontech.ncms.service.PlacementShiftService;
import vn.edu.saigontech.ncms.util.DateUtil;
import vn.edu.saigontech.ncms.web.BaseAction;

@SuppressWarnings("serial")
public class PlacementShiftAction extends BaseAction {
	private Long id;
	private String startTime;
	private String endTime;
	private String comment;
	
	public String list() {
		Collection<PlacementShift> shifts = PlacementShiftService.INSTANCE.findAll();
		List<PlacementShift> list = new ArrayList<PlacementShift>(shifts);
		REQUEST().setAttribute("shifts", list);
		return SUCCESS;
	}
	
	public String showAdd() {
		return SUCCESS;
	}
	
	public String add() {
		PlacementShift shift = new PlacementShift();
		shift.setStartTime(DateUtil.timeFromString(this.startTime, this.i18n("format.time")));
		shift.setEndTime(DateUtil.timeFromString(this.endTime, this.i18n("format.time")));
		shift.setComment(this.comment);
		shift = PlacementShiftService.INSTANCE.persist(shift);
		return SUCCESS;
	}
	
	public String delete() {
		PlacementShiftService.INSTANCE.deleteById(this.id);
		return SUCCESS;
	}
	
	public String showEdit() {
		PlacementShift shift = PlacementShiftService.INSTANCE.findById(this.id);
		REQUEST().setAttribute("shift", shift);
		return SUCCESS;
	}
	
	public String edit() {
		PlacementShift shift = PlacementShiftService.INSTANCE.findById(this.id);
		shift.setStartTime(DateUtil.timeFromString(this.startTime, this.i18n("format.time")));
		shift.setEndTime(DateUtil.timeFromString(this.endTime, this.i18n("format.time")));
		shift.setComment(this.comment);
		shift = PlacementShiftService.INSTANCE.persist(shift);
		return SUCCESS;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}
}